package com.idp.common.model;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.idp.common.utils.ContextUtil;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 分页Model
 * @author king
 * @param <T>
 */
@ApiModel(description = "分页数据")
public class PageModel<T> {

	@ApiModelProperty("当前页码")
	private Integer page;
	@ApiModelProperty("每页条数")
	private Integer rows;
	@ApiModelProperty("数据总条数")
	private Long total;
	@ApiModelProperty("数据结果集")
	private List<T> list;

	public PageModel() {
		super();
		HttpServletRequest request = ContextUtil.getRequest();
		String pageStr = request.getParameter("page");
		if(StrUtil.isNotEmpty(pageStr)) {
			this.page = Integer.parseInt(pageStr);
		}
		String rowsStr = request.getParameter("rows");
		if(StrUtil.isNotEmpty(rowsStr)) {
			this.rows = Integer.parseInt(rowsStr);
		}
		PageHelper.startPage(this.page, this.rows);
	}

	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public Integer getRows() {
		return rows;
	}
	public void setRows(Integer rows) {
		this.rows = rows;
	}
	public void setTotal(Long total) {
		this.total = total;
	}
	public Long getTotal() {
		return total;
	}
	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {

		this.list = list;
		this.total = new PageInfo<T>(list).getTotal();
	}

	public void setResultData(List<T> list, Long total) {

		this.list = list;
		this.total = total;
	}

}
